from marshmallow import Schema, fields, validate
from constant import UserConstant,CommonConstant


class RegisterSchema(Schema):
    username = fields.Str(
        required=True,
        validate=validate.Length(min=2, max=16),
    )
    password = fields.Str(
        required=True,
        validate=validate.Length(min=5, max=16)
    )
    repsd = fields.Str(
        required=False,
    )



class UpdateUserSchema(Schema):
    id = fields.Int(
        required=True,
        validate=validate.Range(min=1)
    )
    username = fields.Str(
        required=False,
        validate=validate.Length(min=2, max=16)
    )
    nickname = fields.Str(
        required=True,
        validate=validate.Length(min=2, max=16)
    )
    email = fields.Str(
        required=True,
        validate=[validate.Email()]
    )
    user_profile = fields.Str(
        required=False,
        validate=validate.Length(min=1)
    )


class UpdatePsdSchema(Schema):
    old_pwd = fields.Str(
        required=True,
    )
    new_pwd = fields.Str(
        required=True,
    )
    re_pwd = fields.Str(
        required=True,
    )


class UserAddSchema(Schema):
    username = fields.Str(
        required=True,
        validate=validate.Length(min=2, max=16),
        allow_none = False

    )
    nickname = fields.Str(
        required=False,
        validate=validate.Length(min=2, max=16),
        allow_none = True
    )
    user_pic = fields.Str(
        required=False,
        validate=validate.Regexp(
            r'^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$',
            error="不是有效的URL"
        ),
        allow_none = True
    )
    user_role = fields.Str(
        required=True,
        validate=validate.OneOf([UserConstant.ADMIN_ROLE, UserConstant.BAN_ROLE, UserConstant.DEFAULT_ROLE]
                                , error="必须是user或者admin或者ban"),
        allow_none = False,
    )


class UserUpdateSchema(Schema):
    id = fields.Int(
        required=True,
        validate=validate.Range(min=1),
        allow_none=False,
    )
    nickname = fields.Str(
        required=False,
        validate=validate.Length(min=2, max=16),
        allow_none = True
    )
    user_pic = fields.Str(
        required=False,
        validate=validate.Regexp(
            r'^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$',
            error="不是有效的URL"
        ),
        allow_none = True
    )
    user_role = fields.Str(
        required=False,
        validate=validate.OneOf([UserConstant.ADMIN_ROLE, UserConstant.BAN_ROLE, UserConstant.DEFAULT_ROLE]
                                , error="必须是user或者admin或者ban"),
        allow_none = False,
    )
    username = fields.Str(
        required=True,
        validate=validate.Length(min=2),
        allow_none = False
    )
    password = fields.Str(
        required=True,
        validate=validate.Length(min=2, max=16),
        allow_none=False,
    )


class ListUserSchema(Schema):
    id = fields.Int(
        required=True,
        validate=validate.Range(min=1)
    )
    nickname = fields.Str(
        required=False,
        validate=validate.Length(min=2, max=16),
        allow_none=True
    )
    user_profile = fields.Str(
        required=False,
        validate=validate.Length(min=1),
        allow_none=True
    )
    user_role = fields.Str(
        required=False,
        validate=validate.OneOf([UserConstant.ADMIN_ROLE, UserConstant.BAN_ROLE, UserConstant.DEFAULT_ROLE]
                                , error="必须是user或者admin或者ban"),
        allow_none = True
    )
    current = fields.Int(
        required=False,
        validate=validate.Range(min=1),
        allow_none=True
    )
    pageSize = fields.Int(
        required=False,
        validate=validate.Range(min=1),
        allow_none=True
    )
    sortField = fields.Str(
        required=False,
        # todo 这里应该是上面得到几个字段之一
        default="id",  # 默认id排序
        validate=validate.OneOf(["id", "nickname", "user_profile", "user_role"]),
        allow_none=True
    )
    sortOrder = fields.Str(
        required=False,
        # 默认升序
        default=CommonConstant.SORT_ORDER_ASC,
        validate=validate.OneOf([CommonConstant.SORT_ORDER_ASC, CommonConstant.SORT_ORDER_DESC]),
        allow_none=True
    )